package com.sec.bnjey;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.WindowManager;
import android.widget.ImageView;

import com.sec.bnjey.bluetooth.BtService;
import com.sec.bnjey.global.GlobalApplication;

public abstract class BaseActivity extends Activity {

	private Timer mTimer = new Timer();
	private Handler mHandler = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		init();
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		super.onCreate(savedInstanceState);
	}

	private void init() {
		mHandler = new Handler() {
			@Override
			public void handleMessage(Message msg) {
				updateTopMenuIcons();
			}
		};

		TimerTask timerTask = new TimerTask() {
			public void run() {
				Message msg = Message.obtain(mHandler, 1, 1, 1, null);
				mHandler.sendMessage(msg);
			}
		};
		mTimer.schedule(timerTask, 500, 500);
	}

	@Override
	protected void onDestroy() {
		mTimer.cancel();
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	protected void updateTopMenuIcons() {
		updateBluetoothStatus(BtService.getBtService().isEnable());
		updateConnectionStatus(BtService.getBtService().isOBDDeviceConnected());
		updateDiagnosisStatus(GlobalApplication.isDtcFound());
	}

	private void updateBluetoothStatus(boolean enabled) {
		ImageView iv;
		iv = (ImageView) findViewById(R.id.IV_TopMenu_Bluetooth);
		iv.setEnabled(enabled);
	}

	private void updateConnectionStatus(boolean enabled) {
		ImageView iv;
		iv = (ImageView) findViewById(R.id.IV_TopMenu_OBD);
		iv.setEnabled(enabled);
	}

	private void updateDiagnosisStatus(boolean enabled) {
		ImageView iv;
		iv = (ImageView) findViewById(R.id.IV_TopMenu_Alert);
		iv.setEnabled(enabled);
	}
}
